
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0008
 * @Author : WangRuoyu 
 * @Date : 2023/5/24 11:18
 */

#include "iostream"

using namespace std;

typedef long long ll;

class Solution {
public:
    int myAtoi(string s) {
        int idx = 0;
        int sign = 1;
        while (idx < s.size() && s[idx] == ' ') {
            idx++;
        }
        if (idx < s.size() && s[idx] == '-') {
            sign = -1;
            idx += 1;
        }
        while (idx < s.size() && !(s[idx] >= '0' && s[idx] <= '9')) {
            idx++;
        }

        ll res = 0;
        for (; idx < s.size(); ++idx) {
            if (res <= ((ll) 1 << 31) && s[idx] >= '0' && s[idx] <= '9') {
                res = res * 10 + s[idx] - '0';
            }
        }
        res *= sign;
        res = min(res, ((ll) 1 << 31) - 1);
        res = max(res, -((ll) 1 << 31));
        return (int) res;
    }
};

int main() {
    Solution sl;
    printf("%d", sl.myAtoi("  -42"));
    return 0;
}